package com.qm.console.controller.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Edison.hu on 18/8/21.
 */
public class BaseResponse<T> extends JSONObject implements Serializable{

    public BaseResponse( JSONObject data) {
        this.put("code",0);
        this.put("msg","success");
        this.put("data",data);
    }

    public BaseResponse(int code,String msg, JSONObject data) {
        this.put("code",code);
        this.put("msg",msg);
        this.put("data",data);
    }

    public BaseResponse(int code,String msg, JSONArray data) {
        this.put("code",code);
        this.put("msg",msg);
        this.put("data",data);
    }

    public BaseResponse(int code,String msg) {
        this.put("code",code);
        this.put("msg",msg);
    }

    public BaseResponse(List<T> list){
        this.put("code",0);
        this.put("msg","success");
        JSONObject data = new JSONObject();
        JSONArray dataItems = new JSONArray();
        list.forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                }
        );
        data.put("items", dataItems);
        this.put("data",data);
    }

    public BaseResponse(JSONArray items){
        this.put("code",0);
        this.put("msg","success");
        JSONObject data = new JSONObject();
        data.put("items", items);
        this.put("data",data);
    }

    public BaseResponse(List<T> list,int count){
        this.put("code",0);
        this.put("msg","success");
        JSONObject data = new JSONObject();
        data.put("count",count);
        JSONArray dataItems = new JSONArray();
        list.forEach(
                x->{
                    dataItems.add(JSONObject.parseObject(JSON.toJSONString(x)));
                }
        );
        data.put("items", dataItems);
        this.put("data",data);
    }

    public BaseResponse(List<String> legendList,List<String> xAxisList,JSONArray seriesList,String title){
        JSONArray axisArray = new JSONArray();
        JSONObject legend = new JSONObject();
        JSONObject xAxis = new JSONObject();
        JSONObject titleName = new JSONObject();
        legend.put("data",legendList);
        xAxis.put("data",xAxisList);
        titleName.put("text",title);
        axisArray.add(xAxis);
        JSONObject data = new JSONObject();
        data.put("title",titleName);
        data.put("legend",legend);
        data.put("xAxis",axisArray);
        data.put("series",seriesList);


        this.put("code",0);
        this.put("msg","success");
        this.put("data",data);
    }

}
